package com.blb.interceptor;

import com.blb.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 前置处理
     * @param request
     * @param response
     * @param handler
     * @return true 放行，false 拦截
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断session中是否有user对象
        User user = (User) request.getSession().getAttribute("user");
        //有就放行请求交给handler处理
        //没有就拦截强制登录
        if(user == null){
            response.sendRedirect("/login");
            return false;
        }
        return true;
    }
}
